package com.luyun.frogpay.base.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;


import com.luyun.frogpay.R;

import java.util.ArrayList;

/**
 * 描述：
 *
 * @author 无人区
 * @date 2019/5/5
 */
public class OptionCenterDialog {
    private AlertDialog albumDialog;
    private ListView dialog_lv;
    private boolean isShowing = false;

    public void show(Context context, ArrayList<String> longData) {
        albumDialog = new AlertDialog.Builder(context).create();
        albumDialog.setCanceledOnTouchOutside(true);
        albumDialog.setCancelable(true);
        View v = LayoutInflater.from(context).inflate(
                R.layout.__picker_dialog_photo_pager, null);
        albumDialog.show();
        albumDialog.setContentView(v);
        albumDialog.getWindow().setBackgroundDrawableResource(R.drawable.__picker_bg_dialog);
        dialog_lv = (ListView) v.findViewById(R.id.dialog_lv);

        CenterDialogAdapter photoDialogAdapter = new CenterDialogAdapter(context, longData);
        dialog_lv.setAdapter(photoDialogAdapter);
        isShowing = true;

        //放在show()之后，不然有些属性是没有效果的，比如height和width
        Window dialogWindow = albumDialog.getWindow();
        WindowManager m = dialogWindow.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        // 设置宽度
        p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.95
        p.gravity = Gravity.CENTER;//设置位置
        //p.alpha = 0.8f;//设置透明度
        dialogWindow.setAttributes(p);
    }

    public void setItemClickListener(AdapterView.OnItemClickListener listener) {
        dialog_lv.setOnItemClickListener(listener);
    }

    public void dismiss(){
        albumDialog.dismiss();
        isShowing = false;
    }

    public boolean isShowing(){
        return isShowing;
    }

}
